package com.stock.stockana.requests;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.stock.publiclib.utils.StringUtil;
import com.stock.stockana.beans.ResponseBean;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.AddStockEvent;
import com.stock.stockana.events.network.SetNotifiEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by ljgsonx on 2016/1/31.
 */
public class SetNotifiPrice extends BaseStringRequest {

    private final static String TAG = "SetNotifiPrice--->";
    private String stockNum;
    private String upTriggerPrice;
    private String downTriggerPrice;
    private String remindStatus;

    private SetNotifiPrice(String stockNum) {
        super(Method.POST, NetWorkConstants.SETNOTIFI_URL, listener, errorListener);
        this.stockNum = stockNum;
        Log.d(TAG, NetWorkConstants.SETNOTIFI_URL);
    }

    static Response.Listener<String> listener = new Response.Listener<String>(){

        @Override
        public void onResponse(String response) {
            try{
                ResponseBean rb = JSON.parseObject(response, ResponseBean.class);
                EventBus.getDefault().post(new SetNotifiEvent(rb));
            }catch (Exception e) {
                ResponseBean rb = new ResponseBean();
                rb.setRespCode(NetWorkConstants.RESPONSE_CODE_ERROR);
                rb.setErrorMsg(NetWorkConstants.RESPONSE_ERROR2 + response);
                EventBus.getDefault().post(new SetNotifiEvent(rb));
            }
        }
    };

    static Response.ErrorListener errorListener = new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            ResponseBean rb = new ResponseBean();
            rb.setRespCode(NetWorkConstants.RESPONSE_CODE_ERROR);
            rb.setErrorMsg(NetWorkConstants.RESPONSE_ERROR3 + error.toString());
            EventBus.getDefault().post(new SetNotifiEvent(rb));
        }
    };

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> map = new HashMap<>();
        map.put("stockNum", stockNum);
        if(!StringUtil.isBlank(upTriggerPrice)) map.put("uptriggerprice", upTriggerPrice);
        if(!StringUtil.isBlank(downTriggerPrice)) map.put("downtriggerprice", downTriggerPrice);
        if(!StringUtil.isBlank(remindStatus)) map.put("remindStatus", remindStatus);
        return map;
    }

    public static SetNotifiPrice build(String stockNum) {
        return new SetNotifiPrice(stockNum);
    }

    public SetNotifiPrice setUpTrigger(String upTrigger) {
        this.upTriggerPrice = upTrigger;
        return this;
    }

    public SetNotifiPrice setDownTrigger(String downTrigger) {
        this.downTriggerPrice = downTrigger;
        return this;
    }

    public SetNotifiPrice setRemindStatus(String remindStatus) {
        this.remindStatus = remindStatus;
        return this;
    }
}
